"""
Case Type   : 事件触发器
Case Name   : 【事件触发器】删除事件触发器使用[CASCADE|RESTRICT]关键字
Create At   :   2024/07/18
Owner       :   sungang14
Description :
    预置条件：
        1.创建事件触发器函数：
        CREATE OR REPLACE FUNCTION f_eventtrigger_0063()
        RETURNS event_trigger
        LANGUAGE plpgsql
        AS $$ BEGIN
        RAISE NOTICE 'test_event_trigger: % %', tg_event, tg_tag;
        END;
        $$;
        2.创建事件触发器
        CREATE EVENT TRIGGER eventtri_eventtrigger_0063 ON ddl_command_start EXECUTE PROCEDURE f_eventtrigger_0063();
        CREATE EVENT TRIGGER eventtri_eventtrigger_0063_1 ON ddl_command_start EXECUTE PROCEDURE f_eventtrigger_0063();
    测试步骤：
        1.使用CASCADE关键字删除存在触发器
        drop EVENT TRIGGER if exists eventtri_eventtrigger_0063 CASCADE;
        2.使用RESTRICT关键字删除存在触发器
        drop EVENT TRIGGER if exists eventtri_eventtrigger_0063_1 RESTRICT;
Expect      : 
        1.删除成功
        2.删除成功
History     : 
"""

import sys
import os
import unittest
from yat.test import Node
from yat.test import macro

sys.path.append(sys.path[0] + "/../")
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH


class IndexFileDamaged(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.logger.info('----------------this is setup-----------------------')
        self.DB_ENV_PATH = macro.DB_ENV_PATH
        self.DB_INSTANCE_PATH = macro.DB_INSTANCE_PATH
        self.comsh = CommonSH('PrimaryDbBmode')
        self.comsh.execut_db_sql("drop EVENT TRIGGER if exists eventtri_eventtrigger_0063;")
        self.comsh.execut_db_sql("drop EVENT TRIGGER if exists eventtri_eventtrigger_0063_1;")
        self.comsh.execut_db_sql("drop function if exists f_eventtrigger_0063();")
        msg = self.comsh.execut_db_sql(
            f"CREATE OR REPLACE FUNCTION f_eventtrigger_0063() RETURNS event_trigger LANGUAGE plpgsql" \
            f" AS \$\$ BEGIN" \
            f" RAISE NOTICE 'test_event_trigger: % %', tg_event, tg_tag;" \
            f"END;" \
            f"\$\$;")
        self.logger.info(msg)
        self.assertIn("CREATE FUNCTION", msg)
        msg = self.comsh.execut_db_sql(
            "CREATE EVENT TRIGGER eventtri_eventtrigger_0063 ON ddl_command_start EXECUTE PROCEDURE f_eventtrigger_0063();")
        self.logger.info(msg)
        self.assertEqual(f'CREATE EVENT TRIGGER', msg)
        msg = self.comsh.execut_db_sql(
            "CREATE EVENT TRIGGER eventtri_eventtrigger_0063_1 ON ddl_command_start EXECUTE PROCEDURE f_eventtrigger_0063();")
        self.logger.info(msg)
        self.assertEqual(f'CREATE EVENT TRIGGER', msg)

    def test_Index_file_damaged(self):
        self.logger.info("--step1:使用CASCADE关键字删除存在触发器")
        msg = self.comsh.execut_db_sql("drop EVENT TRIGGER if exists eventtri_eventtrigger_0063 CASCADE;")
        self.logger.info(msg)
        self.assertEqual('DROP EVENT TRIGGER', msg)
        self.logger.info("--step2:使用RESTRICT关键字删除存在触发器")
        msg = self.comsh.execut_db_sql("drop EVENT TRIGGER if exists eventtri_eventtrigger_0063_1 RESTRICT;")
        self.logger.info(msg)
        self.assertEqual('DROP EVENT TRIGGER', msg)

    def tearDown(self):
        self.logger.info('----------------this is tearDown-----------------------')
        # 删除事件触发器函数
        msg = self.comsh.execut_db_sql("drop function if exists f_eventtrigger_0063();")
        self.logger.info(msg)
        self.assertIn('DROP FUNCTION', msg)
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
